package com.example.study.controller;

import com.example.study.config.JsonResult;
import com.example.study.rabbitmq.config.RabbitmqInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/rabbitmq")
@Api(description = "rabbitmq模块")
public class RabbitmqController {

    @Resource
    private RabbitTemplate rabbitTemplate;
    //    // 1: 定义交换机
//    private String exchangeName = "fanout_order_exchange";
//    // 2: 路由key
//    private String routeKey = "";
    @Resource
    RabbitmqInfo rabbitmqInfo;//相关参数

    @GetMapping("/sendMessage")
    @ApiOperation("发送消息接口")
    public JsonResult<String> sendMessage(@RequestParam @ApiParam("要发的消息") String msg) {
        JsonResult<String> result = new JsonResult<>();
        try {
            rabbitTemplate.convertAndSend(RabbitmqInfo.exchangeName, "chatKey", msg);
            result.data="发送成功";
        } catch (AmqpException e) {
            e.printStackTrace();
            result.data = e.getMessage();
            result.code = 101;
        }

        return result;
    }

}
